<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物理卡号" prop="icCardPhysicalId">
        <el-input
          v-model="queryParams.icCardPhysicalId"
          placeholder="请输入物理卡号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="IC卡号" prop="icCardNo">
        <el-input
          v-model="queryParams.icCardNo"
          placeholder="请输入IC卡号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申报状态" prop="icCardStatus">
        <el-select v-model="queryParams.icCardStatus" @keyup.enter.native="handleQuery" style="width: 120px"
                   placeholder="请选择申报状态">
          <el-option v-for="item in dict.type.ic_card_type" :key="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申报时间" prop="ddate">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kmwlpts:icCardProfile:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['kmwlpts:icCardProfile:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['kmwlpts:icCardProfile:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          :loading="exportLoading"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['kmwlpts:icCardProfile:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-upload"
          size="mini"
          :loading="exportLoading"
          @click="handleDeclare"
          v-hasPermi="['kmwlpts:icCardProfile:declare']"
        >申报
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="icCardProfileList" @selection-change="handleSelectionChange"
              :row-class-name="tableRowClassName" border>
      <el-table-column type="selection" width="55" align="center" :selectable="selectable"/>
      <!--      <el-table-column label="主键" align="center" prop="id" v-show="false"/>-->
      <el-table-column label="IC卡号" align="center" prop="icCardNo"/>
      <el-table-column label="物理卡号" align="center" prop="icCardPhysicalId"/>
      <el-table-column label="IC卡类型" align="center" prop="icCardType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ic_card_type" :value="scope.row.icCardType"/>
        </template>
      </el-table-column>

      <el-table-column label="IC卡状态" align="center" prop="icCardStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_ic_isopen" :value="scope.row.icCardStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="创建人" align="center" prop="createdByActor"/>
      <el-table-column label="创建时间" align="center" prop="createdDatetime" width="180">
        <template slot-scope="scope">

          <span>{{ parseTime(scope.row.createdDatetime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updatedByActor"/>
      <el-table-column label="修改时间" align="center" prop="updatedDatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedDatetime) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="版本" align="center" prop="versionStamp" />-->

      <el-table-column label="企业编码" align="center" prop="firmCode"/>
      <!--      <el-table-column label="部门ID" align="center" prop="departmentId" />-->
      <el-table-column label="申报时间" align="center" prop="ddate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ddate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="dStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_ic_status" :value="scope.row.dStatus"/>
        </template>

      </el-table-column>
      <el-table-column label="占用对象" align="center" prop="bindingBusinessObj">
        <template slot-scope="scope">
          <span v-if="scope.row.bindingBusinessObj">{{ scope.row.bindingBusinessObj }}</span>
          <span v-else>
            未占用
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kmwlpts:icCardProfile:edit']"
            v-if="['1','5','6'].includes(scope.row.dStatus)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kmwlpts:icCardProfile:remove']"
            v-if="scope.row.dStatus==='1'"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['kmwlpts:icCardProfile:query']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改IC卡备案管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="IC卡号" prop="icCardNo">
              <el-input v-model="form.icCardNo" placeholder="请输入IC卡号"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物理卡号" prop="icCardPhysicalId">
              <el-input v-model="form.icCardPhysicalId" placeholder="请输入物理卡号"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="IC卡类型" prop="icCardType">
              <el-select v-model="form.icCardType" placeholder="请选择IC卡类型">
                <el-option v-for="item in cardStatusOptions" :key="item.value" :value="item.value"
                           :label="item.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" @click="readIcCard">读 卡</el-button>
            <el-button type="success" @click="wirteIcCard">写 卡</el-button>

          </el-col>

        </el-row>


        <el-form-item label="创建时间" prop="createdDatetime" v-show="false">
          <el-date-picker clearable size="small"
                          v-model="form.createdDatetime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改时间" prop="updatedDatetime" v-show="false">
          <el-date-picker clearable size="small"
                          v-model="form.updatedDatetime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择修改时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改人" prop="updatedByActor" v-show="false">
          <el-input v-model="form.updatedByActor" placeholder="请输入修改人"/>
        </el-form-item>
        <el-form-item label="创建人" prop="createdByActor" v-show="false">
          <el-input v-model="form.createdByActor" placeholder="请输入创建人"/>
        </el-form-item>
        <el-form-item label="版本" prop="versionStamp" v-show="false">
          <el-input v-model="form.versionStamp" placeholder="请输入版本"/>
        </el-form-item>

        <el-form-item label="IC卡状态" v-show="false">
          <el-radio-group v-model="form.icCardStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业编码" prop="firmCode" v-show="false">
          <el-input v-model="form.firmCode" placeholder="请输入企业编码"/>
        </el-form-item>
        <el-form-item label="部门ID" prop="departmentId" v-show="false">
          <el-input v-model="form.departmentId" placeholder="请输入部门ID"/>
        </el-form-item>
        <el-form-item label="申报时间" prop="ddate" v-show="false">
          <el-date-picker clearable size="small"
                          v-model="form.ddate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择申报时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" v-show="false">
          <el-radio-group v-model="form.dStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业申请单ID" prop="copAppNo" v-show="false">
          <el-input v-model="form.copAppNo" placeholder="请输入企业申请单ID"/>
        </el-form-item>
        <el-form-item label="场站号" prop="tradeCo" v-show="false">
          <el-input v-model="form.tradeCo" placeholder="请输入场站号"/>
        </el-form-item>
        <el-form-item label="区代码" prop="customsCode" v-show="false">
          <el-input v-model="form.customsCode" placeholder="请输入区代码"/>
        </el-form-item>
        <el-form-item label="发送编号" prop="senderCode" v-show="false">
          <el-input v-model="form.senderCode" placeholder="请输入发送编号"/>
        </el-form-item>
        <el-form-item label="备注" prop="note" v-show="false">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog :title="title" :visible.sync="openDetail" width="70%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="8">
            <el-form-item label="物理卡号" prop="icCardPhysicalId">
              <el-input v-model="form.icCardPhysicalId" placeholder="" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="IC卡号" prop="icCardNo">
              <el-input v-model="form.icCardNo" placeholder="" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="IC卡类型" prop="icCardType">
              <!--          <el-select v-model="form.icCardType" placeholder="" :disabled="true">-->
              <!--            <el-option-->
              <!--              v-for="dict in dict.type.IC_TYPE"-->
              <!--              :key="dict.value"-->
              <!--              :label="dict.label"-->
              <!--              :value="dict.value"-->
              <!--            ></el-option>-->
              <!--          </el-select>-->
              <dict-tag :options="dict.type.IC_TYPE" :value="form.icCardType"/>

            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="IC卡状态:" prop="icCardStatus">
              <!--          <el-select v-model="form.icCardStatus" placeholder="" :disabled="true">-->
              <!--            <el-option-->
              <!--              v-for="dict in dict.type.sys_ic_isopen"-->
              <!--              :key="dict.value"-->
              <!--              :label="dict.label"-->
              <!--              :value="dict.value"-->
              <!--            ></el-option>-->
              <!--          </el-select>-->
              <dict-tag :options="dict.type.sys_ic_isopen" :value="form.icCardStatus"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="企业编码" prop="firmCode">
              <el-input v-model="form.firmCode" placeholder="" :disabled="true"/>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="审核状态:" prop="dStatus">
              <!--              <el-input v-model="form.dStatus" placeholder="" :disabled="true"/>-->
              <dict-tag :options="dict.type.sys_ic_status" :value="form.dStatus"/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="创建时间：" label-width="100px">{{ parseTime(form.createdDatetime) }}</el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="创建人：" label-width="100px">
              <el-input v-model="form.createdByActor" placeholder="" :disabled="true"/>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="修改时间：" label-width="100px">{{ parseTime(form.updatedDatetime) }}</el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="修改人：" label-width="100px">
              <el-input v-model="form.createdByActor" placeholder="" :disabled="true"/>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="申报时间：" label-width="100px">{{ parseTime(form.updatedDatetime) }}</el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="区代码：" label-width="100px">
              <el-input v-model="form.customsCode" placeholder="" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>


        </el-row>

        <el-col :span="8">
          <el-form-item label="备注" prop="note">
            <el-input v-model="form.note" type="textarea" placeholder="" :disabled="true"/>
          </el-form-item>
        </el-col>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" @click="goBack">返 回</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<style>
.el-table .use-row {
  background: #eee;
}

.el-table .no-use-row {
  background: #fffae8;
}
</style>
<script>
import {
  listIcCardProfile,
  getIcCardProfile,
  delIcCardProfile,
  addIcCardProfile,
  updateIcCardProfile,
  exportIcCardProfile,
  checkIcCardRepeat,
  declareIcCardProfile
} from "@/api/kmwlpts/icCardProfile";
import {getIcCardInfo, writeIcCardInfo} from '@/utils/iccard'

export default {
  name: "IcCardProfile",
  dicts: ['IC_TYPE', 'sys_ic_isopen', 'ic_card_type', 'sys_ic_status'],
  data() {
    return {
      dateRange: [],
      //查看IC卡备案弹出层
      openDetail: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // IC卡备案管理表格数据
      icCardProfileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        icCardPhysicalId: null,
        icCardNo: null,
        icCardType: null,
        icCardStatus: null,
        ddate: null,
        dStatus: null,
        tradeCo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        icCardNo: [
          {required: true, message: "IC卡号不能为空", trigger: "blur"},
          // {min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur'}
        ],
      },
      cardStatusOptions: [
        {label: "长期卡", value: "L"},
        {label: "临时卡", value: "T"}
      ],
      noDecls: ["1", "5"]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({row, rowIndex}) {
      if (row.bindingBusinessObj) {
        return 'use-row';
      } else {
        return 'no-use-row';
      }
    },
    handleClick(tab, event) {
    },
    /** 查询IC卡备案管理列表 */
    getList() {
      this.loading = true;
      listIcCardProfile(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.icCardProfileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createdDatetime: null,
        updatedDatetime: null,
        updatedByActor: null,
        createdByActor: null,
        versionStamp: null,
        icCardPhysicalId: null,
        icCardNo: null,
        icCardType: null,
        icCardStatus: "0",
        firmCode: null,
        departmentId: null,
        ddate: null,
        dStatus: "0",
        copAppNo: null,
        tradeCo: null,
        customsCode: null,
        senderCode: null,
        note: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增IC卡备案管理";
      this.form.icCardType = this.cardStatusOptions[0].value;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getIcCardProfile(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改IC卡备案管理";
        this.form.icCardType = this.cardStatusOptions[0].value;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          checkIcCardRepeat(this.form).then(res => {
            if (res.code !== 1 && res.code < 0) {
              this.$modal.alertError(res.msg);
              return false;
            } else {
              if (this.form.id != null) {
                updateIcCardProfile(this.form).then(response => {
                  this.$modal.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              } else {
                addIcCardProfile(this.form).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }

            }
          })


        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除IC卡备案管理编号为"' + ids + '"的数据项？').then(function () {
        return delIcCardProfile(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有IC卡备案管理数据项？').then(() => {
        this.exportLoading = true;
        return exportIcCardProfile(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    //读取IC卡
    readIcCard() { //读卡
      getIcCardInfo().then(data => {
        if (data.result == "200") {
          this.form.icCardNo = data.data.icno;
        } else {
          this.$modal.msgError("读卡失败！");
        }
      });
    },
    wirteIcCard() {
      if (!this.form.icCardNo) {
        this.$modal.msgWarning('不能写入空卡号!');
        return;
      }
      writeIcCardInfo({
        icCard: this.form.icCardNo
      }).then(data => {
        if (data.result == "200") {
          this.$message.warning('写卡成功！');
        } else {
          this.$message.warning('写卡失败！');
        }

      });
    },
    selectable(row, index) {
      if (this.noDecls.includes(row.dStatus)) {
        return true;
      } else {
        return false;
      }

    },
    //IC卡备案申报
    handleDecl() {

    },
    /**申报*/
    handleDeclare(row) {
      const ids = row.id || this.ids;
      if (ids == undefined || ids == null || ids == "") {
        this.$modal.msgError("请先勾选要申报的IC卡备案数据！");
        return false;
      }
      this.$modal.confirm('是否确认申报IC卡备案管理编号为"' + ids + '"的数据项？').then(function () {
        return declareIcCardProfile(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("申报成功");
      }).catch(() => {
      });
    },
    handleDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getIcCardProfile(id).then(response => {
        this.form = response.data;
        this.openDetail = true;
        this.title = "IC卡备案详情";

      });
    },
    goBack() {
      this.openDetail = false;
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-input.is-disabled ::v-deep .el-input__inner {
  color: #606266;
}

.el-textarea.is-disabled ::v-deep .el-textarea__inner {
  color: #606266;
}

#testCar ::v-deep .DisableSelection > .cell {
  display: none !important;
}
</style>
